package com.ctbri.vSearch.dao.es.multiThread;

import com.ctbri.vSearch.dao.ES2Redis;
import com.ctbri.vSearch.util.CountScore;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.springframework.stereotype.Repository;

import com.ctbri.vSearch.bean.po.ArticleItem;
import com.ctbri.vSearch.frame.ESDataPool;
import redis.clients.jedis.Jedis;

/***
 * ES查询Dao
 * @author wkhuahuo
 *
 */
@Repository("articleItemSearchDao")
public class ESSearchDao {
	
	
	@Resource
	ESDataPool esDataPool;
	
	String index ="articles";
	
	public List<ArticleItem> searchArticle( Map<ArticleItem,Double> articleScore,Map<ArticleItem, HashMap<String, Float>> termScore, String key,String word,Double weight ,Jedis jedis) {
		TransportClient client = esDataPool.getCurrentConnection();
        ES2Redis es2r = new ES2Redis();
		SearchResponse sr = client.prepareSearch(key)//"articles_spider"
                 .setQuery(QueryBuilders.queryStringQuery(word))
                 .setFrom(0)
                 .setSize(10)
                 .get();
		Iterator<SearchHit> iter = sr.getHits().iterator();

		List<ArticleItem> result = new ArrayList<ArticleItem>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();

        while(iter.hasNext())
        {
            SearchHit sh = (SearchHit) iter.next();
            ArticleItem ai = new ArticleItem();
            ai.setId(sh.getId());
            ai.setContent((String)sh.getSource().get("content"));
            ai.setOffcial_account((String)sh.getSource().get("official_account"));
            ai.setTitle((String)sh.getSource().get("title"));
            ai.setUrl((String)sh.getSource().get("url"));
            String dat = (String)sh.getSource().get("date");
            if(null==dat){dat = (String)sh.getSource().get("time");}
            ai.setDate(dat);
            Date date = null;
            try {
                date = dateFormat.parse(dat);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //计算分数，从代码中独立出来
            CountScore.countScore(date, articleScore, termScore, ai, sh.getScore(), word, weight);
            //创建该word的ES查询结果的redis缓存标志
            es2r.createES2RedisByWord(word,jedis);
            try {
                System.out.println("Redis正在缓存："+word);
                //将ES查询结果存入redis中
                es2r.putArticle2Redis(sh.getId(),(String)sh.getSource().get("content"),
                        (String)sh.getSource().get("official_account"),
                        (String)sh.getSource().get("title"),
                        (String)sh.getSource().get("url"),
                        (String)sh.getSource().get("date"),
                        (String)sh.getSource().get("time"),
                        sh.getScore(),jedis,word); //这里直接将score存入redis,回避计算score必须查询ES数据库的缺陷
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        esDataPool.releaseConnection(client);
		return result;
	}
}
